package com.kuaiditu.assistant.ui.balance;

import android.graphics.Color;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.kuaiditu.assistant.R;
import com.kuaiditu.assistant.api.assistant_api.AssistantApi;
import com.kuaiditu.assistant.api.assistant_api.base.AssistantResponse;
import com.kuaiditu.assistant.base.BaseActivity;
import com.kuaiditu.assistant.entity.Balance;
import com.kuaiditu.assistant.entity.BalanceProcess;
import com.kuaiditu.assistant.module.retrofit.ProgressSubscriber;
import com.kuaiditu.assistant.ui.dialog.TaskInfoDialog;
import com.kuaiditu.assistant.util.ToastUtil;
import com.kuaiditu.assistant.view.topbar.TopBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.iwgang.simplifyspan.SimplifySpanBuild;
import cn.iwgang.simplifyspan.unit.SpecialTextUnit;

@Route(path = BalanceActivity.PATH)
public class BalanceActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {
    public static final String PATH = "/user/balance";
    @BindView(R.id.top_bar)
    TopBar topBar;
    @BindView(R.id.tv_title_balance)
    TextView tvTitleBalance;
    @BindView(R.id.tv_title_balance_count)
    TextView tvTitleBalanceCount;
    @BindView(R.id.tv_deposit)
    TextView tvDeposit;
    @BindView(R.id.tv_total_balance)
    TextView tvTotalBalance;
    @BindView(R.id.tv_frost_count)
    TextView tvFrostCount;
    @BindView(R.id.rv_balance)
    RecyclerView rvBalance;
    @BindView(R.id.swiper)
    SwipeRefreshLayout swiper;

    AssistantApi mAssistantApi;

    int page = 1;

    BalanceAdapter mBalanceAdapter;

    List<BalanceProcess> mBalanceProcesses = new ArrayList<>();

    Balance mBalance;

    @Override
    public int initContentView() {
        return R.layout.activity_balance;
    }

    @Override
    public void initInjector() {
        mAssistantApi = getApplicationComponent().getApiManager().getAssistantApi();
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        topBar.bind(this, true);


        swiper.setOnRefreshListener(this);


        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        rvBalance.setLayoutManager(linearLayoutManager);
        rvBalance.setAdapter(mBalanceAdapter = new BalanceAdapter(mBalanceProcesses));
        rvBalance.addItemDecoration(new DividerItemDecoration(rvBalance.getContext(), linearLayoutManager.getOrientation()));

        onRefresh();


    }


    @OnClick(R.id.tv_deposit)
    public void onViewClicked() {

        if (mBalance != null) {
            showDialog();
        } else {
            ToastUtil.showToast("提现失败，请刷新");
        }

    }






    private void fetchBalance() {
        mAssistantApi.fetchBalance(page)
                .subscribe(new ProgressSubscriber<Balance>(mContext) {
                    @Override
                    public void onStart() {

                    }

                    @Override
                    public void onNext(Balance balance) {
                        mBalance = balance;
                        tvTotalBalance.setText(String.valueOf(balance.getFreeze()));
                        tvFrostCount.setText(String.valueOf(0.00D));
                        tvTitleBalanceCount.setText(String.valueOf(balance.getTotalMoney()));

                        mBalanceProcesses.clear();
                        mBalanceProcesses.addAll(balance.getListProcess());
                        mBalanceAdapter.notifyDataSetChanged();

                    }

                    @Override
                    public void onFinish() {
                        swiper.setRefreshing(false);
                    }
                });

    }

    @Override
    public void onRefresh() {
        page = 1;
        fetchBalance();
        swiper.setRefreshing(true);
    }



    private void showDialog() {

         View info =  LayoutInflater.from(mContext).inflate(R.layout.dialog_deposit_layout, null);

        final EditText editDep = (EditText) info.findViewById(R.id.edit_deposit_count);
        TextView tvHint = (TextView) info.findViewById(R.id.tv_alert_text);

        SimplifySpanBuild hintBuild = new SimplifySpanBuild();

        hintBuild.append(new SpecialTextUnit("提现金额会提现到以下支付宝账号\n", Color.parseColor("#999999")))
                .append(new SpecialTextUnit(mBalance.getAliCode(), getResources().getColor(R.color.colorPrimary)));
        tvHint.setText(hintBuild.build());

        TaskInfoDialog taskInfoDialog = (TaskInfoDialog) ARouter.getInstance().build(TaskInfoDialog.PATH).navigation();
        taskInfoDialog.setCustomView(info);

        taskInfoDialog.setOnTakeTaskClickListener("确定", new TaskInfoDialog.onTakeTaskClickListener() {
            @Override
            public void onClick() {
                double money = Double.valueOf(editDep.getText().toString());
                if (money >= 100) {
                    if (money <= mBalance.getTotalMoney()) {
                        deposit(String.valueOf(money));
                    } else {
                        ToastUtil.showToast("余额不足");
                    }

                } else {
                    ToastUtil.showToast("提现金额需大于100");
                }




            }
        });
        taskInfoDialog.show(getSupportFragmentManager(), TaskInfoDialog.PATH);
    }



    private void deposit(String count) {

        mAssistantApi.deposit(count)
                .subscribe(new ProgressSubscriber<AssistantResponse>(mContext) {
                    @Override
                    public void onNext(AssistantResponse assistantResponse) {
                        onRefresh();
                    }

                    @Override
                    public void onFinish() {

                    }
                });

    }
}
